package com.zhdl.modules.logging.service.impl;

import com.project.common.service.impl.BaseServiceImpl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.zhdl.modules.logging.entity.OptRecord;
import com.zhdl.modules.logging.entity.QOptRecord;
import com.zhdl.modules.logging.repository.OptRecordRepository;
import com.zhdl.modules.logging.service.OptRecordService;
import com.zhdl.modules.logging.web.req.OptRecordListRequest;
import com.zhdl.modules.logging.web.resp.OptRecordListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class OptRecordServiceImpl extends BaseServiceImpl<String, OptRecord, OptRecordRepository> implements OptRecordService {

    @Resource
    private OptRecordRepository optRecordRepository;


    /**
     * 保存记录
     *
     * @param optRecord 记录信息
     * @return 记录Id
     */
    public String saveOptRecord(OptRecord optRecord){
        OptRecord save = optRecordRepository.save(optRecord);
        return save.getId();
    }

    /**
     * 更新记录
     */
    public String updateOptRecord(OptRecord optRecord){
        OptRecord save = optRecordRepository.save(optRecord);
        return save.getId();
    }

    /**
     * 查询某一条
     *
     * @param optId 记录Id
     * @return 记录对象
     */
    public OptRecord findByOptId(String optId){
        Optional<OptRecord> byId = optRecordRepository.findById(optId);
        return byId.orElse(null);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<OptRecord> findAllOptId(){
        return optRecordRepository.findAll();
    }


    @Override
    public Page<OptRecordListResp> findAllRecordPage(OptRecordListRequest request, Pageable pageable) {
        QOptRecord qOptRecord = QOptRecord.optRecord;

        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qOptRecord.createDate.between(request.getStartTime(), request.getEndTime()));

        if (request.getUser().size() > 0){
            BooleanBuilder builder1 = new BooleanBuilder();
            for (String s : request.getUser()) {
                builder1.or(qOptRecord.user.eq(s));
            }
            builder.and(builder1);
        }


        JPAQuery<OptRecordListResp> query = jpaQueryFactory()
                .select(Projections.constructor(OptRecordListResp.class,
                        qOptRecord.id,
                        qOptRecord.user,
                        qOptRecord.optName,
                        qOptRecord.createDate
                ))
                .from(qOptRecord)
                .where(builder);

        List<OptRecordListResp> fetch = query
                .orderBy(qOptRecord.createDate.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetch();

        long total = query.fetchCount();

        return new PageImpl<>(fetch, pageable, total);
    }

    @Override
    public void save(OptRecord optRecord){
        optRecordRepository.save(optRecord);
    }

    @Override
    public void saveRecords(String remarks){
        OptRecord optRecord = new OptRecord();
        UUID uuid = UUID.randomUUID();
        optRecord.setId(uuid.toString());
        optRecord.setUser("用户");
        optRecord.setOptName(remarks);
        save(optRecord);
    }
}
